package com.example.songyingjie

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.content.Intent
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat


class MainActivity : AppCompatActivity() {
    private lateinit var num1EditText: EditText
    private lateinit var num2EditText: EditText
    private lateinit var operationTextView: TextView
    private lateinit var resultTextView: TextView
    private lateinit var operationButton: Button
    private lateinit var historyButton: Button


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        num1EditText = findViewById(R.id.num1EditText)
        num2EditText = findViewById(R.id.num2EditText)
        operationTextView = findViewById(R.id.operationTextView)
        resultTextView = findViewById(R.id.resultTextView)
        operationButton = findViewById(R.id.operationButton)
        historyButton = findViewById(R.id.historyButton)

        num1EditText.setTextColor(ContextCompat.getColor(this, R.color.blue))
        num2EditText.setTextColor(ContextCompat.getColor(this, R.color.blue))
        operationTextView.setTextColor(ContextCompat.getColor(this, R.color.blue))
        resultTextView.setTextColor(ContextCompat.getColor(this, R.color.blue))

        operationButton.setOnClickListener {
            performOperation()
        }

        historyButton.setOnClickListener {
            startActivity(Intent(this, HistoryActivity::class.java))
        }
    }

    private fun performOperation() {
        val num1 = num1EditText.text.toString().toDoubleOrNull()
        val num2 = num2EditText.text.toString().toDoubleOrNull()
        val operation = operationTextView.text.toString()

        if (num1 != null && num2 != null) {
            when (operation) {
                "+" -> {
                    val result = num1 + num2
                    resultTextView.text = "$result"
                    saveHistory("$num1 + $num2 = $result")
                }
                "-" -> {
                    val result = num1 - num2
                    resultTextView.text = "$result"
                    saveHistory("$num1 - $num2 = $result")
                }
                "*" -> {
                    val result = num1 * num2
                    resultTextView.text = "$result"
                    saveHistory("$num1 * $num2 = $result")
                }
                "/" -> {
                    if (num2 != 0.0) {
                        val result = num1 / num2
                        resultTextView.text = "$result"
                        saveHistory("$num1 / $num2 = $result")
                    } else {
                        resultTextView.text = "除数不能为0"
                    }
                }
            }
        } else {
            resultTextView.text = "请输入数字"
        }
    }

    private fun saveHistory(history: String) {
        val sharedPref = getSharedPreferences("CalculatorPrefs", MODE_PRIVATE)
        with(sharedPref.edit()) {
            val currentHistory = sharedPref.getStringSet("history", mutableSetOf())
            val newHistory = currentHistory?.toMutableSet() ?: mutableSetOf()
            newHistory.add(history)
            putStringSet("history", newHistory)
            apply()
        }
    }
}